import Vue from 'vue'
import Vuex from 'vuex';
//import ajax from '../js/ajax.js'
import ajax from 'axios'
import { Notification } from "element-ui"
require('es6-promise').polyfill();
	Vue.use(Vuex)
     let state={
          tableData:[],
          devLoading:false,
          linkTable:[],
          linkLoading:false,
          username:'',
          str:''
      }
     let mutations={
        userEdit(state,username){
          state.username=username;
        },
        //设备删除
        deviceremove(state,tableData){
            state.tableData.splice(tableData,1);
        },
        //数据初始
        tableDatanew(state,tableData){
          state.tableData=tableData;
        },
        //设备增加
        deviceadd(state,tableData){
            state.tableData.push(tableData);
        },
        //设备编辑
        deviceEdit(state,tableData){
            let removeEdit=tableData.rows.rows;
            if(removeEdit<=0){
              removeEdit=1;
            }
            state.tableData.splice(tableData.rows.rows,removeEdit,tableData.list);
        },
        //设备管理loading事件
        devLoading(state,loading){
        	state.devLoading=loading
        },
        //------------------------联动管理--------------------------------//
        //初始化
        linkrestart(state,linkTable){
            state.linkTable=linkTable;
        },
        //联动删除
        linkremove(state,linkTable){
            state.linkTable.splice(linkTable,1);
        },
        //联动新增
        linkadd(state,linkTable){
          state.linkTable.push(linkTable);
        },
        linkEdit(state,linkTable){
            let removeEdit=linkTable.row.row;
            if(removeEdit<=0){
              removeEdit=1;
            }
            state.linkTable.splice(linkTable.row.row,removeEdit,linkTable.list);
        },
        linkLoading(state,loading){
          state.linkloading=loading
        },
        set_log(state,str){//用户操作日志
            let  msg= {"cmd":"CGI_LOG_APPEND_USER","content":str};
            $.ajax({
                url:"cgi-bin/main.cgi",
                contentType: "application/json;charset=utf-8",
                type:"post",
                data:JSON.stringify(msg),
                success:function(msg){
                }
            })
        }
      }

      let actions={
        inremove(context,tableData){
            let msg = new Object();
            msg["cmd"] = "CGI_DEVICE_SET_DELETE";
            msg["deviceid"] = tableData.deviceid;
            console.log(msg);
            if(window.sessionStorage.getItem('username')=="zyl"){
                ajax.get("../../static/request.json",msg).then(function(res){
                    if(eval('(' + res.data + ')').response==true){
                        context.commit('deviceremove',tableData.index);
                        context.commit('devLoading',false);
                        Notification({
                            title: '成功',
                            message: '操作成功！',
                            type: 'success',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }else{
                        context.commit('devLoading',false);
                        Notification({
                            title: '失败',
                            message: '操作失败！',
                            type: 'error',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }
              })

            }else{
                ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                    if(res.data.response==true){
                        context.commit('deviceremove',tableData.index);
                        context.commit('devLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                        context.commit('set_log',"删除设备信息");
                    }else{
                        context.commit('devLoading',false);
                        Notification({
                          title: '失败',
                          message: res.data.errlog,
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }
        },
        deviceAdd(context,tableData){
            //向msg插入cmd属性;
           /*let msg = new Object();
            msg = tableData;
            msg["cmd"] = "CGI_DEVICE_SET_APPEND";
            console.log(msg); */

            let str = {"cmd":"CGI_DEVICE_SET_APPEND","sensortype":tableData.sensorid,"pluginid":tableData.pluginid,"deviceid":tableData.deviceid,"intertype":tableData.intertype,"interid":tableData.interid,"address":parseInt(tableData.address),"name":tableData.name,"supplier":tableData.supplier,"model":tableData.model};
             let msg = JSON.stringify(str);
            console.log(msg);
          if(window.sessionStorage.getItem('username')=="zyl"){
              ajax.get("../../static/request.json",msg).then(function(res){
                  if(eval('(' + res.data + ')').response==true){
                      context.commit('deviceadd',tableData);
                      context.commit('devLoading',false);
                      Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                      });


                  }else{
                      context.commit('devLoading',false);
                      Notification({
                          title: '失败',
                          message: '操作失败！',
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                      });
                  }
              })
           }else{
              ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                  if(res.data.response==true){
                      context.commit('deviceadd',tableData);
                      context.commit('devLoading',false);
                      Notification({
                        title: '成功',
                        message: '操作成功！',
                        type: 'success',
                        position:'bottom-right',
                        duration:'1500'
                      });
                      context.commit('set_log',"添加设备信息");
                  }else{
                      context.commit('devLoading',false);
                      Notification({
                        title: '失败',
                        message: res.data.errlog,
                        type: 'error',
                        position:'bottom-right',
                        duration:'1500'
                      });
                  }
              })

           }
        },
        deviceEdit(context,tableData){
            let msg  = new Object();
            msg = tableData.list;
            msg["cmd"] = "CGI_DEVICE_SET_MODIFY";
            console.log(msg);
            if(window.sessionStorage.getItem('username')=="zyl"){
                ajax.get("../../static/request.json",msg).then(function(res){
                if(eval('(' + res.data + ')').response==true){
                  context.commit('deviceEdit',tableData);
                  context.commit('devLoading',false);
                  Notification({
                            title: '成功',
                            message: '操作成功！',
                            type: 'success',
                            position:'bottom-right',
                            duration:'1500'
                          });

                    }else{
                        context.commit('devLoading',false);
                        Notification({
                            title: '失败',
                            message: '操作失败！',
                            type: 'error',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }
                })
            }else{
                ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                    if(res.data.response==true){
                        context.commit('deviceEdit',tableData);
                        context.commit('devLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                        context.commit('set_log',"修改设备信息");
                    }else{
                        context.commit('devLoading',false);
                        Notification({
                          title: '失败',
                          message:res.data.errlog,
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }
        },
        linkRemove(context,linkTable){
            let msg = {"cmd":"CGI_LINKAGE_SET_DELETE","linkid":linkTable.linkid};
            if(window.sessionStorage.getItem('username')=="zyl"){
                ajax.get("./static/request.json",msg).then(function(res){
                    if(eval('(' + res.data + ')').response==true){
                        context.commit('linkremove',linkTable.linkIndex);
                        context.commit('linkLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }else{
                        Notification({
                          title: '失败',
                          message: '操作失败！',
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }else{
                ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                   if(res.data.response==true){
                        context.commit('linkremove',linkTable.linkIndex);
                        context.commit('linkLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }else{
                        Notification({
                          title: '失败',
                          message: '操作失败！',
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }
        },
        linkAdd(context,linkTable){
            //向msg插入cmd属性;
            let msg = new Object();
            msg = linkTable;
            msg["cmd"] = "CGI_LINKAGE_SET_APPEND";

            if(window.sessionStorage.getItem('username')=="zyl"){
                ajax.get("./static/request.json",msg).then(function(res){
                    let data = eval('(' + res.data + ')');
                    //将成功返回的的linkid用于web中
                    msg['linkid'] = data.linkid;
                    if(data.response==true){
                        context.commit('linkadd',msg);
                        context.commit('linkLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }else{
                        Notification({
                          title: '失败',
                          message: '操作失败！',
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }else{
                ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                    //将成功返回的的linkid用于web中
                    msg['linkid'] = res.data.linkid;
                    if(res.data.response==true){
                        context.commit('linkadd',msg);
                        context.commit('linkLoading',false);
                        Notification({
                          title: '成功',
                          message: '操作成功！',
                          type: 'success',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }else{
                        Notification({
                          title: '失败',
                          message:res.data.errlog,
                          type: 'error',
                          position:'bottom-right',
                          duration:'1500'
                        });
                    }
                })
            }
        },
        linkEdit(context,linkTable){
            let msg = new Object();
            msg = linkTable.list;
            msg["cmd"] = "CGI_LINKAGE_SET_MODIFY";
            console.log(msg);
            if(window.sessionStorage.getItem('username')=="zyl"){
                ajax.get("./static/request.json",msg).then(function(res){
                    if(eval('(' + res.data + ')').response==true){
                        context.commit('linkEdit',linkTable);
                        context.commit('linkLoading',false);
                        Notification({
                            title: '成功',
                            message: '操作成功！',
                            type: 'success',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }else{
                        context.commit('devLoading',false);
                        Notification({
                              title: '失败',
                              message: '操作失败！',
                              type: 'error',
                              position:'bottom-right',
                              duration:'1500'
                        });
                    }
                })
            }else{
                ajax.post("../cgi-bin/main.cgi",msg).then(function(res){
                   if(res.data.response==true){
                        context.commit('linkEdit',linkTable);
                        context.commit('linkLoading',false);
                        Notification({
                            title: '成功',
                            message: '操作成功！',
                            type: 'success',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }else{
                        context.commit('devLoading',false);
                        Notification({
                            title: '失败',
                            message:res.data.errlog,
                            type: 'error',
                            position:'bottom-right',
                            duration:'1500'
                        });
                    }
                })
            }
        }
      }

export default new Vuex.Store({
  state,
  actions,
  mutations
});
